package zuk.clipvietfun.clipadapter;

import java.util.ArrayList;
import java.util.List;

import zuk.clipvietfun.R;
import zuk.clipvietfun.loadimageview.ImageLoader;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ClipAdapter extends ArrayAdapter<Clip> {
	ImageLoader imageLoader;
	private final TextView mFooter;
	private List<Clip> clips;
	private List<Clip> tempList = new ArrayList<Clip>();
	private List<Clip> listClip;
	private boolean mHasMoreItems;
	private String fileTxt;
	int i = 2;
	private int mLastOffset = 0;
	private int LIST_SIZE;

	public List<Clip> getClips() {
		return clips;
	}

	public ClipAdapter(Context context, int textViewResourceId, String fileTxt,
			List<Clip> clipsub, List<Clip> listClip, int mLastOffset,
			int LIST_SIZE,TextView mFooter) {
		super(context, textViewResourceId, clipsub);
		clips = clipsub;
		this.fileTxt = fileTxt;
		imageLoader = new ImageLoader(context);
		this.mLastOffset = mLastOffset;
		this.LIST_SIZE = LIST_SIZE;
		this.listClip = listClip;
		mHasMoreItems = true;
		this.mFooter = mFooter;
	}

	public int getmLastOffset() {
		return mLastOffset;
	}

	public void setmLastOffset(int mLastOffset) {
		this.mLastOffset = mLastOffset;
	}
	public void notifyNoMoreItems(){
        mHasMoreItems = false;
        mFooter.setText("No more Items");
    }
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;

		if (position == getCount() - 1 && mHasMoreItems) {
			tempList.clear();
			int lastOffset = getmLastOffset();
			if (lastOffset < LIST_SIZE) {
				int limit = lastOffset + 20;
				for (int i = (lastOffset + 1); (i <= limit && i < LIST_SIZE); i++) {
					tempList.add(listClip.get(i));
				}
				setmLastOffset(limit);
				if (limit < LIST_SIZE) {
					mHasMoreItems = true;
				}
				else{
					mHasMoreItems = false;
				}
				mFooter.setText("Loading ...");
				new LoadClipEnlessAdapterTask(getContext(), this)
				.execute(tempList);
			}
		}

		if (v == null) {
			LayoutInflater infalInflater = (LayoutInflater) getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = infalInflater.inflate(R.layout.clip, null);
		}
		ImageHolder imgHolder = new ImageHolder();
		TextView tViewTitle = (TextView) v.findViewById(R.id.text_title);

		Clip clip = clips.get(position);
		tViewTitle.setText(Html.fromHtml(clip.getTitle()));

		imgHolder.img_thumb = (ImageView) v.findViewById(R.id.img_thumb);
		imgHolder.img_thumb.setTag(position);
		imgHolder.prgBar = (ProgressBar) v.findViewById(R.id.prgBar);
		imgHolder.img_url = clip.imgurl;
		imgHolder.position = position;
		imageLoader.DisplayImage(imgHolder.img_url, imgHolder.img_thumb,
				imgHolder.prgBar);
		// new LoadImageTask(imgHolder).execute();
		return v;
	}

}
